package com.linkage.event.controller;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.linkage.common.base.RequestInfo;
import com.linkage.common.base.ResponseInfo;
import com.linkage.event.pojo.entity.TrainMessage;
import com.linkage.event.service.TrainMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 培训管理
 */
@RestController
@RequestMapping(value = "/train")
@Slf4j
public class TrainMessageController {

    @Resource
    private TrainMessageService trainMessageService;

    /**
     * 分页查询培训列表
     * @param messageRequestInfo
     * @return
     */
    @PostMapping("/queryListByPage")
    public String queryListByPage(@RequestBody RequestInfo<TrainMessage> messageRequestInfo){
        int page = StrUtil.isEmpty(messageRequestInfo.getBase().getPageNum())?1:Integer.parseInt(messageRequestInfo.getBase().getPageNum());
        int pageSize = StrUtil.isEmpty(messageRequestInfo.getBase().getPageRows())?10:Integer.parseInt(messageRequestInfo.getBase().getPageRows());
        PageInfo pageInfo = trainMessageService.queryListByPage(messageRequestInfo.getBuss(),page,pageSize);
        return ResponseInfo.successWithPage(pageInfo).toString();
    }

    /**
     * 新增培训信息
     * @param messageRequestInfo
     * @return
     */
    @PostMapping("/insertTrainInfo")
    public String insertTrainInfo(@RequestBody RequestInfo<TrainMessage> messageRequestInfo){
        trainMessageService.insertTrainInfo(messageRequestInfo.getBuss());
        return ResponseInfo.success("新增成功").toString();
    }

    /**
     * 修改培训信息
     * @param messageRequestInfo
     * @return
     */
    @PostMapping("/updateTrainInfo")
    public String updateTrainInfo(@RequestBody RequestInfo<TrainMessage> messageRequestInfo){
        trainMessageService.updateTrainInfo(messageRequestInfo.getBuss());
        return ResponseInfo.success("修改成功").toString();
    }

    /**
     * 查询培训信息详情
     * @param messageRequestInfo
     * @return
     */
    @PostMapping("/queryTrainInfo")
    public String queryTrainInfo(@RequestBody RequestInfo<TrainMessage> messageRequestInfo){
        if(StrUtil.isEmpty(messageRequestInfo.getBuss().getId())){
            return ResponseInfo.error("id参数为空！").toString();
        }
        TrainMessage message = trainMessageService.queryTrainInfo(messageRequestInfo.getBuss().getId());
        return ResponseInfo.success(message).toString();
    }

    /**
     * 删除培训信息
     * @param messageRequestInfo
     * @return
     */
    @PostMapping("/deleteTrainInfo")
    public String deleteTrainInfo(@RequestBody RequestInfo<TrainMessage> messageRequestInfo){
        if(StrUtil.isEmpty(messageRequestInfo.getBuss().getId())){
            return ResponseInfo.error("id参数为空！").toString();
        }
        trainMessageService.delete(messageRequestInfo.getBuss().getId());
        return ResponseInfo.success("删除成功！").toString();
    }
}
